package com.kun.video.ui.pane;

import cn.hutool.core.lang.RegexPool;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.kun.video.domain.KunFileWrapper;
import com.kun.video.ui.control.KunTextField;
import com.kun.video.ui.control.button.KunButton;
import com.kun.video.ui.dialog.message.PopupMessage;
import com.kun.video.util.ComUtil;
import com.kun.video.util.CommandExecutor;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.io.File;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * TODO
 *
 * @author gzc
 * @since 2025/5/16
 **/
public class RemoveFixedWatermarkBox extends HBox {
    private final KunImageViewBox imageViewBox = new KunImageViewBox(true);

    private final KunTextField kunTextField = new KunTextField();
    private final KunButton kunButton = new KunButton("加载");
    private final TitleVerBox buttonBox = new TitleVerBox("选择", new Label("选择第几秒："), kunTextField, kunButton);

    private final File videoFile;

    public RemoveFixedWatermarkBox(File videoFile) {
        this.videoFile = videoFile;
        this.initDimensions();
        this.initLayout();
    }

    public void initDimensions() {
//        setPrefSize(1000, 800);
        imageViewBox.setPrefSize(800, 800);
    }

    public void initLayout() {
        imageViewBox.setOffset(10, 10);
        setStyle("-fx-background-color: rgba(209, 240, 246, 0.16)");
        buttonBox.setStyle("-fx-background-color: rgba(212, 224, 245, 0.2)");
        buttonBox.setPadding(new Insets(10));
        buttonBox.setAlignment(Pos.TOP_CENTER);
        this.kunButton.action(() -> {
            String text = kunTextField.getText();
            if (!ReUtil.isMatch(RegexPool.NUMBERS, text)) {
                return;
            }
            if (this.videoFile != null) {
                imageViewBox.loadVideoFrameImage(this.videoFile, Integer.parseInt(text));
            }
        });
        getChildren().addAll(imageViewBox, this.buttonBox);
    }


    public void resultHandler(Consumer<String> updateTextArea, BiConsumer<String, String> updateProgress) {
        double[] selection = imageViewBox.getSelection();
        if (ArrayUtil.isEmpty(selection) || selection.length < 4) {
            new PopupMessage().error("未框选水印的坐标");
            return;
        }
        KunFileWrapper kunFileWrapper = new KunFileWrapper(this.videoFile);
        File outputFile = kunFileWrapper.getOutputFile("qsy");
        if (!ComUtil.isOverwriteOutputFileBlockThread(outputFile)) {
            return;
        }

        String delogoStr = StrUtil.format("delogo=x={}:y={}:w={}:h={}:show=0", selection[0], selection[1], selection[2], selection[3]);
        String commandStr = "ffmpeg -i {} -vf \"{}\" -threads 4 -thread_queue_size 512 -preset slower -c:v libx264 -y {}";
        CommandExecutor.executeCommand(StrUtil.format(commandStr, kunFileWrapper.getAbsolutePath(), delogoStr, kunFileWrapper.getOutputFile("qsy")), (line) -> {
            updateTextArea.accept(line);
            updateProgress.accept("去水印2.0【" + kunFileWrapper.getInputFileName() + "】", line);
        });
    }
}
